/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.ParsedContact;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CreateContact
/*     */   extends RedoableOp
/*     */ {
/*     */   private int mId;
/*     */   private int mFolderId;
/*     */   private Map<String, String> mFields;
/*     */   private ParsedContact mParsedContact;
/*     */   private RedoableOpData mRedoLogContent;
/*     */   private String[] mTags;
/*     */   private String mTagIds;
/*     */   
/*     */   public CreateContact()
/*     */   {
/*  57 */     super(MailboxOperation.CreateContact);
/*  58 */     this.mId = 0;
/*  59 */     this.mFolderId = 0;
/*     */   }
/*     */   
/*     */   public CreateContact(int mailboxId, int folderId, ParsedContact pc, String[] tags) {
/*  63 */     this();
/*  64 */     setMailboxId(mailboxId);
/*  65 */     this.mId = 0;
/*  66 */     this.mFolderId = folderId;
/*  67 */     this.mFields = pc.getFields();
/*  68 */     this.mParsedContact = pc;
/*  69 */     this.mTags = (tags != null ? tags : new String[0]);
/*     */   }
/*     */   
/*     */   public void setContactId(int id) {
/*  73 */     this.mId = id;
/*     */   }
/*     */   
/*     */   public int getContactId() {
/*  77 */     return this.mId;
/*     */   }
/*     */   
/*     */   protected String getPrintableData()
/*     */   {
/*  82 */     StringBuffer sb = new StringBuffer("folder=").append(this.mFolderId);
/*  83 */     sb.append(", tags=\"").append(TagUtil.encodeTags(this.mTags)).append("\"");
/*  84 */     if ((this.mFields != null) && (this.mFields.size() > 0)) {
/*  85 */       sb.append(", attrs={");
/*  86 */       for (Map.Entry<String, String> entry : this.mFields.entrySet()) {
/*  87 */         String key = (String)entry.getKey();
/*  88 */         String value = (String)entry.getValue();
/*  89 */         sb.append("\n    ").append(key).append(": ").append(value);
/*     */       }
/*  91 */       sb.append("\n}");
/*     */     }
/*  93 */     return sb.toString();
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out) throws IOException
/*     */   {
/*  98 */     out.writeInt(this.mId);
/*  99 */     out.writeInt(this.mFolderId);
/* 100 */     out.writeShort((short)-1);
/* 101 */     if (getVersion().atLeast(1, 33)) {
/* 102 */       out.writeUTFArray(this.mTags);
/*     */     } else {
/* 104 */       out.writeUTF(this.mTagIds);
/*     */     }
/* 106 */     int numAttrs = this.mFields != null ? this.mFields.size() : 0;
/* 107 */     out.writeShort((short)numAttrs);
/* 108 */     if (numAttrs > 0) {
/* 109 */       for (Map.Entry<String, String> entry : this.mFields.entrySet()) {
/* 110 */         out.writeUTF((String)entry.getKey());
/* 111 */         String value = (String)entry.getValue();
/* 112 */         out.writeUTF(value != null ? value : "");
/*     */       }
/*     */     }
/* 115 */     if (getVersion().atLeast(1, 14)) {
/* 116 */       out.writeInt((int)this.mParsedContact.getSize());
/*     */     }
/*     */   }
/*     */   
/*     */   public InputStream getAdditionalDataStream() throws IOException
/*     */   {
/* 122 */     if (getVersion().atLeast(1, 14)) {
/* 123 */       if (this.mParsedContact != null)
/* 124 */         return this.mParsedContact.getContentStream();
/* 125 */       if (this.mRedoLogContent != null) {
/* 126 */         return this.mRedoLogContent.getInputStream();
/*     */       }
/*     */     }
/* 129 */     return null;
/*     */   }
/*     */   
/*     */   protected void deserializeData(RedoLogInput in) throws IOException {
/* 133 */     this.mId = in.readInt();
/* 134 */     this.mFolderId = in.readInt();
/* 135 */     in.readShort();
/* 136 */     if (getVersion().atLeast(1, 33)) {
/* 137 */       this.mTags = in.readUTFArray();
/*     */     } else {
/* 139 */       this.mTagIds = in.readUTF();
/*     */     }
/* 141 */     int numAttrs = in.readShort();
/* 142 */     if (numAttrs > 0) {
/* 143 */       this.mFields = new HashMap(numAttrs);
/* 144 */       for (int i = 0; i < numAttrs; i++) {
/* 145 */         String key = in.readUTF();
/* 146 */         String value = in.readUTF();
/* 147 */         this.mFields.put(key, value);
/*     */       }
/*     */     }
/* 150 */     if (getVersion().atLeast(1, 14)) {
/* 151 */       int length = in.readInt();
/* 152 */       if (length > 104857600)
/* 153 */         throw new IOException("deserialized message size too large (" + length + " bytes)");
/* 154 */       if (length > 0) {
/* 155 */         this.mRedoLogContent = new RedoableOpData(new File(in.getPath()), in.getFilePointer(), length);
/*     */         
/*     */ 
/* 158 */         long pos = in.getFilePointer();
/* 159 */         int numSkipped = in.skipBytes(length);
/* 160 */         if (numSkipped != length) {
/* 161 */           String msg = String.format("Attempted to skip %d bytes at position %d in %s, but actually skipped %d.", new Object[] { Integer.valueOf(length), Long.valueOf(pos), in.getPath(), Integer.valueOf(numSkipped) });
/*     */           
/* 163 */           throw new IOException(msg);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void redo() throws Exception {
/* 170 */     int mboxId = getMailboxId();
/* 171 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(mboxId);
/* 172 */     OperationContext octxt = getOperationContext();
/*     */     
/* 174 */     if ((this.mTags == null) && (this.mTagIds != null)) {
/* 175 */       this.mTags = TagUtil.tagIdStringToNames(mbox, octxt, this.mTagIds);
/*     */     }
/*     */     
/* 178 */     InputStream in = null;
/*     */     try {
/* 180 */       in = getAdditionalDataStream();
/* 181 */       ParsedContact pc = new ParsedContact(this.mFields, in);
/* 182 */       mbox.createContact(octxt, pc, this.mFolderId, this.mTags);
/*     */     } catch (ServiceException e) {
/* 184 */       String code = e.getCode();
/* 185 */       if (code.equals("mail.ALREADY_EXISTS")) {
/* 186 */         mLog.info("Contact %d already exists in mailbox %d", new Object[] { Integer.valueOf(this.mId), Integer.valueOf(mboxId) });
/*     */       } else {
/* 188 */         throw e;
/*     */       }
/*     */     } finally {
/* 191 */       ByteUtil.closeStream(in);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/CreateContact.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */